/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.collection.CollUtil
 *  cn.hutool.core.util.ObjectUtil
 *  cn.hutool.core.util.StrUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.CheckBox
 *  javafx.scene.control.TableCell
 *  javafx.scene.control.TableColumn
 *  javafx.scene.control.TableView
 *  javafx.scene.control.TextField
 *  javafx.scene.control.cell.PropertyValueFactory
 *  javafx.scene.layout.AnchorPane
 *  javafx.util.Callback
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.medCheck;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.ProductBasicUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.enums.ProForm;
import com.gys.controller.BaseController;
import com.gys.controller.medCheck.MedCheckController;
import com.gys.data.medCheck.GetMedCheckDOutData;
import com.gys.data.productBasic.GetProductInfoQueryInData;
import com.gys.data.productBasic.GetProductInfoQueryOutData;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class MedCheckProductController
extends BaseController
implements Initializable {
    @FXML
    private AnchorPane anchorPane;
    @FXML
    private TextField searchProId;
    @FXML
    private TableView<GetProductInfoQueryOutData> tableView;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, CheckBox> checkBox;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> proCode;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> proName;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> batchNo;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> stockQty;
    private ObservableList<GetProductInfoQueryOutData> observableTableList = FXCollections.observableArrayList(new ArrayList());
    @Autowired
    private MedCheckController medCheckController;

    public void initialize(URL location, ResourceBundle resources) {
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isNull(this.anchorPane)) {
            return;
        }
        this.searchProId.setText("");
        this.observableTableList.clear();
        this.bingTableCell();
        this.tableView.setItems(this.observableTableList);
    }

    @FXML
    private void search() {
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        if (StrUtil.isBlank((CharSequence)this.searchProId.getText().trim())) {
            alert.setContentText("\u5fc5\u987b\u81f3\u5c11\u586b\u5199\u4e00\u9879\u5185\u5bb9\u8fdb\u884c\u67e5\u8be2\uff01");
            alert.showAndWait();
            return;
        }
        GetProductInfoQueryInData inData = new GetProductInfoQueryInData();
        inData.setGspgProId(this.searchProId.getText().trim());
        HttpJson rsp = ProductBasicUtil.selectMedCheckProList(inData);
        if (rsp.isSuccess()) {
            List<GetProductInfoQueryOutData> dataList = JSON.parseArray((String)rsp.getMsg(), GetProductInfoQueryOutData.class);
            int index = 1;
            for (GetProductInfoQueryOutData data : dataList) {
                data.setIndex(index);
                ++index;
            }
            this.observableTableList.clear();
            this.observableTableList = FXCollections.observableArrayList((Collection)dataList);
            this.bingTableCell();
            this.tableView.setItems(this.observableTableList);
        } else {
            alert.setContentText(rsp.getMsg());
            alert.showAndWait();
        }
    }

    private void bingTableCell() {
        this.checkBox.setCellValueFactory(cellData -> ((GetProductInfoQueryOutData)cellData.getValue()).getCheckBox().getCustomCheckBox());
        this.checkBox.setCellFactory(col -> {
            TableCell<GetProductInfoQueryOutData, CheckBox> cell = new TableCell<GetProductInfoQueryOutData, CheckBox>(){

                protected void updateItem(CheckBox item, boolean empty) {
                    super.updateItem(item, empty);
                    this.setGraphic((Node)item);
                    if (item != null) {
                        item.setOnMouseClicked(col -> {
                            if (CollUtil.isEmpty((Collection)MedCheckProductController.this.observableTableList)) {
                                return;
                            }
                            int index = this.getIndex();
                            for (int i = 0; i < MedCheckProductController.this.observableTableList.size(); ++i) {
                                if (i == index) continue;
                                ((GetProductInfoQueryOutData)MedCheckProductController.this.observableTableList.get(i)).getCheckBox().setSelectStatus(false);
                            }
                        });
                    }
                }
            };
            return cell;
        });
        this.proName.setCellValueFactory((Callback)new PropertyValueFactory(this.proName.getId()));
        this.proCode.setCellValueFactory((Callback)new PropertyValueFactory(this.proCode.getId()));
        this.batchNo.setCellValueFactory((Callback)new PropertyValueFactory(this.batchNo.getId()));
        this.stockQty.setCellValueFactory((Callback)new PropertyValueFactory(this.stockQty.getId()));
        this.tableView.getSelectionModel().selectedItemProperty().addListener((ChangeListener)new ChangeListener<GetProductInfoQueryOutData>(){

            public void changed(ObservableValue<? extends GetProductInfoQueryOutData> observable, GetProductInfoQueryOutData oldValue, GetProductInfoQueryOutData newValue) {
                if (CollUtil.isEmpty((Collection)MedCheckProductController.this.observableTableList)) {
                    return;
                }
                for (int i = 0; i < MedCheckProductController.this.observableTableList.size(); ++i) {
                    if (i == newValue.getIndex() - 1) {
                        ((GetProductInfoQueryOutData)MedCheckProductController.this.observableTableList.get(i)).getCheckBox().setSelectStatus(true);
                        continue;
                    }
                    ((GetProductInfoQueryOutData)MedCheckProductController.this.observableTableList.get(i)).getCheckBox().setSelectStatus(false);
                }
            }
        });
    }

    @FXML
    private void add() {
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        List<Integer> indexList = this.getSelectIndex();
        if (CollUtil.isEmpty(indexList)) {
            alert.setContentText("\u8bf7\u9009\u62e9\u8bb0\u5f55");
            alert.showAndWait();
            return;
        }
        GetProductInfoQueryOutData proData = (GetProductInfoQueryOutData)this.observableTableList.get(indexList.get(0) - 1);
        if (ObjectUtil.isNull(proData)) {
            alert.setContentText("\u8bf7\u9009\u62e9\u8bb0\u5f55");
            alert.showAndWait();
            return;
        }
        ObservableList<GetMedCheckDOutData> list = this.medCheckController.getObservableDetailTableList();
        for (GetMedCheckDOutData data : list) {
            if (!data.getGsmcdProId().equals(proData.getProCode()) || !data.getGsmcdBatchNo().equals(proData.getBatchNo())) continue;
            alert.setContentText(StrUtil.format((CharSequence)"\u6240\u9009\u5546\u54c1({})\u6279\u53f7({})\u5df2\u5b58\u5728\uff0c\u4e0d\u53ef\u91cd\u65b0\u6dfb\u52a0", (Object[])new Object[]{data.getGsmcdProId(), data.getGsmcdBatchNo()}));
            alert.showAndWait();
            return;
        }
        GetMedCheckDOutData outData = new GetMedCheckDOutData();
        outData.setDetailIndex(list.size() + 1);
        outData.setGsmcdProId(proData.getProCode());
        outData.setProName(proData.getProName());
        outData.setProSpecs(proData.getProSpecs());
        outData.setProUnit(proData.getProUnit());
        outData.setGsmcdBatchNo(proData.getBatchNo());
        outData.setVaild(proData.getVaild());
        outData.setStockQty(proData.getStockQty());
        outData.setGsmcdQty(proData.getStockQty());
        outData.setGsmcdResult("1");
        outData.setProFactoryName(proData.getProFactoryName());
        outData.setProPlace(proData.getProPlace());
        if (StrUtil.isNotBlank((CharSequence)proData.getProForm())) {
            outData.setProForm(ProForm.getName(proData.getProForm()));
        }
        this.medCheckController.addProduct(outData);
        this.closeWindow();
    }

    @FXML
    private void closeWindow() {
        this.anchorPane.getScene().getWindow().hide();
    }

    private List<Integer> getSelectIndex() {
        ArrayList<Integer> ids = new ArrayList<Integer>();
        if (CollUtil.isNotEmpty(this.observableTableList)) {
            for (GetProductInfoQueryOutData i : this.observableTableList) {
                if (!i.getCheckBox().isSelected().booleanValue()) continue;
                ids.add(i.getIndex());
            }
        }
        return ids;
    }
}

